﻿import QtQuick 2.6
import QtQuick.Window 2.2
import QtQuick.Controls 2.2
import "test.js" as Utils

ApplicationWindow {
    visible: true
    width: 400
    height: 400
    title: "C++ & QML Demo"

    Column {
        anchors.centerIn: parent
        spacing: 20
        
        
        TextField {
            id: input1
            placeholderText: "输入数字1"
        }

        TextField {
            id: input2
            placeholderText: "输入数字2"
        }

        Text {
            text: myObject.message  // 显示C++属性
            font.pixelSize: 18
        }
        
        Text {
            id: resultText
            text: "结果: "
            font.pixelSize: 16
            
        }

        Button {
            text: "Update Message"
            onClicked: {
                myObject.updateMessage()  // 调用C++方法
                console.log("[QML] Button clicked")
                console.log("Current file: " + Qt.resolvedUrl("qrc:/main.qml"))
                let sum = calculateSum(input1.text, input2.text);
                resultText.text = "结果: " + sum;
                
                console.log(Utils.add(3, 5));  // 输出：8
                var calc = new Utils.Calculator();
                console.log(calc.multiply(2, 4));  // 输出：8
            }
        }
        
    }
    // 定义 JavaScript 函数
    function calculateSum(a, b) {
        return Number(a) + Number(b);
    }
}